//
//  MainViewController.swift
//  FirstSinaProject-00
//
//  Created by mac on 16/4/7.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
       let mainTabbar = HMMainTabbar()
        setValue(mainTabbar, forKey: "tabBar")
        addChildViewController()
        setupComposeBtn()
        

    }
// MARK: - 懒加载控件
    private lazy var composeButton: UIButton = {
        let button = UIButton()
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_button_add_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Normal)
//        button.sizeToFit()
        return button
    }()

// MARK: - 添加子控件
    private func addChildViewController() {
//        tabBar.tintColor = UIColor.orangeColor()
        addChildViewController(HomeViewController(), title: "首页", image: "tabbar_home")
        addChildViewController(DiscoverTableViewController(), title: "发现", image: "tabbar_discover")
        addChildViewController(MessageTableViewController(), title: "消息", image: "tabbar_message_center")
        addChildViewController(ProfilleTableViewController(), title: "我", image: "tabbar_profile")

    
    }

}
extension MainViewController{
    private func setupComposeBtn(){
        tabBar.addSubview(composeButton)
        let count = childViewControllers.count
        
        let width  = tabBar.bounds.width / CGFloat(count) - 1
        composeButton.frame = CGRectInset(tabBar.bounds, 2 * width, 0)
        
    }
    
    private func addChildViewController(vc: UIViewController, title: String, image: String) {
        
        vc.title = title
        vc.tabBarItem.image = UIImage(named: image)
        vc.tabBarItem.selectedImage = UIImage(named: image + "_highlighted")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: UIControlState.Selected)
        addChildViewController(UINavigationController(rootViewController: vc))
    }
    
}









